Ext.ns('Wlanauth.user');
/**
 * 用户分类列表
 */
Wlanauth.user.TabPanel = Ext.extend(Ext.Panel,{
	title:'用户资料列表',
	tabPanel:null,
	searchGrid:null,
	iconCls:'usertype',
	btnDelHidden:true,
	btnEditHidden:true,
	btnSwitchStatusHidden:true,
	btnExcelHidden:true,
	btnMoveToHis:false,
	constructor:function(config){
	
		Ext.apply(this,config);
		
		this.btnDelHidden = !ZZExt.ApplicationContext.actionAccessable('wlanauth.user.delete');
		this.btnEditHidden = !ZZExt.ApplicationContext.actionAccessable('wlanauth.user.edit');
		this.btnSwitchStatusHidden = !ZZExt.ApplicationContext.actionAccessable('wlanauth.user.switchstatus');
		this.btnExcelHidden = !ZZExt.ApplicationContext.actionAccessable('wlanauth.user.excel');
		//this.btnMoveToHis = !ZZExt.ApplicationContext.actionAccessable('wlanauth.user.excel');
		
		this.tabPanel = new Ext.TabPanel({
			activeTab:0,
			defaults:{xtype:'wlanauth.user.gridpanel'},
			border:false,
			activedTabIds:[],//用户记录哪些曾被激活过
			items:[{
				title:'正常用户',
				storeUrl:'user!list.action?entity.status=NORMAL',
				userStatusType:'NORMAL',
				tbar:[{
					text:'删除',
					iconCls:'delete',
					handler:this.deleteObject,
					hidden:this.btnDelHidden
				},'-',{
					text:'修改',
					iconCls:'edit',
					handler:this.editObject,
					hidden:this.btnEditHidden
				},'-',{
					text:'停/启用',
					iconCls:'class',
					handler:this.switchStatus,
					hidden:this.btnSwitchStatusHidden
				},'->',{
					text:'导出',
					iconCls:'toExcel',
					status:'NORMAL',
					handler:this.toExcel,
					hidden:this.btnExcelHidden
				}]
			},{
				title:'到期用户',
				
				storeUrl:'user!list.action?entity.status=EXPIRED',
				userStatusType:'EXPIRED',
				tbar:[{
					text:'删除',
					iconCls:'delete',
					handler:this.deleteObject,
					hidden:this.btnDelHidden
				},'-',{
					text:'修改',
					iconCls:'edit',
					hidden:this.btnEditHidden,
					handler:this.editObject
				},'-',{
					text:'转为历史用户',
					iconCls:'item',
					handler:this.moveToHis
				},'->',{
					text:'导出',
					iconCls:'toExcel',
					handler:this.toExcel,
					hidden:this.btnExcelHidden
				},{
					text:'全部转为历史用户',
					iconCls:'movetoHisAll',
					handler:this.moveAllToHis,
					hidden:this.btnMoveToHis
				}]
			},{
				title:'流量用完用户',
				storeUrl:'user!list.action?entity.status=NONEOCT',
				userStatusType:'NONEOCT',
				tbar:[{
					text:'删除',
					iconCls:'delete',
					handler:this.deleteObject,
					hidden:this.btnDelHidden
				},'-',{
					text:'修改',
					iconCls:'edit',
					handler:this.editObject,
					hidden:this.btnEditHidden
				},'-',{
					text:'转为历史用户',
					iconCls:'item',
					handler:this.moveToHis,
					hidden:this.btnMoveToHis
				},'->',{
					text:'导出',
					iconCls:'toExcel',
					handler:this.toExcel,
					hidden:this.btnExcelHidden
				},{
					text:'全部转为历史用户',
					iconCls:'movetoHisAll',
					handler:this.moveAllToHis,
					hidden:this.btnMoveToHis
				}]
			},{
				title:'历史用户',
				iconCls:'class',
				userStatusType:'HISTORY',
				storeUrl:'userHis!list.action',
				tbar:['-','历史数据不能编辑,如需编辑,请联系管理员','->',{
					text:'导出',
					iconCls:'toExcel',
					handler:this.toExcel,
					hidden:this.btnExcelHidden
				}]
			},{
				title:'在线用户',
				iconCls:'class',
				storeUrl:'user!list.action?entity.status=ONLINE',
				userStatusType:'ONLINE',
				tbar:[{
					text:'停/启用',
					iconCls:'class',
					handler:this.switchStatus,
					hidden:this.btnSwitchStatusHidden
				},'->',{
					text:'导出',
					iconCls:'toExcel',
					status:'NORMAL',
					handler:this.toExcel,
					hidden:this.btnExcelHidden
				}]
			}]
			
		});
		Wlanauth.user.TabPanel.superclass.constructor.call(this,{
			items:this.tabPanel,
			border:false,
			layout:'fit',
			tbar:['输入相关条件查询(匹配模式:全匹配)','->','用户范围','-',
			      {xtype:'radio',name:'userRange',boxLabel:'在网&nbsp',checked:true},
			      {xtype:'radio',name:'userRange',boxLabel:'历史&nbsp'},'-',
			      {xtype:'localcombo',
			    	  data:[['批次号','entity.batchId'],['卡号','entity.username'],['绑定IP','entity.bindIP']],
			    	  value:'entity.batchId'},
			      {xtype:'textfield',width:200},'-',
			      {text:'查找',iconCls:'search',handler:this.doSearch,scope:this}]
			
		});
		this.tabPanel.on('tabchange',function(_this,panel){
			
//			var id = panel.getId();
//			for(var i=0;i<_this.activedTabIds.length;i++){
//				if(_this.activedTabIds[i]==id||id=='userTab-search'){
//					return;
//				}
//			}
			var id = panel.getId();
			if(id=='userTab-search') return ;
			panel.store.reload();
			//_this.activedTabIds.push(id)
		});
	},
	toExcel:function(){
	
		this.ownerCt.ownerCt.toExcel();
	},
	editObject:function(){
		this.ownerCt.ownerCt.fireEvent('rowdblclick');
	},
	deleteObject:function(){
		this.ownerCt.ownerCt.deleteObject();
	},
	switchStatus:function(){
		this.ownerCt.ownerCt.switchStatus();
	},
	moveToHis:function(){
		this.ownerCt.ownerCt.moveToHis();
	},
	moveAllToHis:function(){
		this.ownerCt.ownerCt.moveAllToHis();
	},
	//查询
	doSearch:function(){
		var bar = this.getTopToolbar();
		 
		var sType=bar.findByType('textfield')[0].getValue();   
   		var sKey=bar.findByType('textfield')[1].getValue();
   		var p={};
   		if(sKey.length>0){
       		p[sType] = sKey;
   		}
   		var userType = bar.findByType('radio')[0].getValue();	
		var _url = 'user!list.action';
   		var tbar = [{
			text:'删除',
			iconCls:'delete',
			handler:this.deleteObject,
			hidden:this.btnDelHidden
		},'-',{
			text:'修改',
			iconCls:'edit',
			hidden:this.btnEditHidden,
			handler:this.editObject
		},'-',{
			text:'停/启用',
			iconCls:'class',
			handler:this.switchStatus,
			hidden:this.btnSwitchStatusHidden
		},'->',{
			text:'导出',
			iconCls:'toExcel',
			handler:this.toExcel,
			hidden:this.btnExcelHidden
		}];
   		var userStatusType='NORMAL';
		if(!userType){ //历名用户
			_url = 'userHis!list.action';
			userStatusType='HISTORY';
			tbar = ['-','历史数据不能编辑,如需编辑,请联系管理员','->',{
				text:'导出',
				iconCls:'toExcel',
				handler:this.toExcel,
				hidden:this.btnExcelHidden
			}];
		}
		
   		if(this.searchGrid == null||this.searchGrid.userStatusType!=userStatusType){
			if(this.searchGrid!=null){
				this.tabPanel.remove(this.searchGrid);
			}
			
			this.searchGrid = new Wlanauth.user.GridPanel({
				title:'查询结果',
				id:'userTab-search',
				tbar:tbar,
				userStatusType:userStatusType
			});
			this.tabPanel.add(this.searchGrid);
		}
		this.tabPanel.setActiveTab(this.searchGrid);
		
		var store = this.searchGrid.store;
		store.proxy = new Ext.data.HttpProxy( {
			url : _url
		});
		p['start'] = 0;
		p['limit'] = store.baseParams.limit;
		p['entity.status']= userStatusType;
		store.baseParams = {};
		Ext.apply(store.baseParams,p);
		store.load();
		
	}
	
});

Ext.reg('wlanauth.user.tabpanel',Wlanauth.user.TabPanel);